package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.dto.CustomerGuestFamilyDetailDTO;
import com.prd.mapper.CustomerGuestFamilyDetailMapper;
import com.prd.model.CustomerGuestFamilyDetailDO;
import com.prd.service.CustomerGuestFamilyDetailService;
import com.prd.tools.CopyNonNullBeanUtils;
import com.prd.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 客户-客人家庭详情总表 服务实现类
 * </p>
 *
 * @author o1
 * @since 2023-12-14
 */
@Service("CustomerGuestFamilyDetailService")
public class CustomerGuestFamilyDetailServiceImpl extends ServiceImpl<CustomerGuestFamilyDetailMapper, CustomerGuestFamilyDetailDO> implements CustomerGuestFamilyDetailService {

    @Autowired
    private CustomerGuestFamilyDetailMapper guestFamilyDetailMapper;

    @Override
    public JsonData uUpdateAndAdd(CustomerGuestFamilyDetailDTO familyDetailDTO) {
        LambdaQueryWrapper<CustomerGuestFamilyDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestFamilyDetailDO::getGuestNo, familyDetailDTO.getGuestNo());
        CustomerGuestFamilyDetailDO familyDetailDO = guestFamilyDetailMapper.selectOne(wrapper);
        if (familyDetailDO == null) {
            familyDetailDO = new CustomerGuestFamilyDetailDO();
            CopyNonNullBeanUtils.copyProperties(familyDetailDTO, familyDetailDO);
            guestFamilyDetailMapper.insert(familyDetailDO);
        }
        else {
            CopyNonNullBeanUtils.copyProperties(familyDetailDTO, familyDetailDO);
            guestFamilyDetailMapper.update(familyDetailDO, wrapper);
        }
        return JsonData.buildSuccess(familyDetailDO);
    }

    @Override
    public JsonData uSearch(String guestNo) {
        if (guestNo == null)
            return JsonData.buildError("guestNo参数为空");
        return JsonData.buildSuccess(search(guestNo));
    }

    @Override
    public CustomerGuestFamilyDetailDTO search(String guestNo) {
        LambdaQueryWrapper<CustomerGuestFamilyDetailDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestFamilyDetailDO::getGuestNo, guestNo);
        CustomerGuestFamilyDetailDO familyDetailDO = guestFamilyDetailMapper.selectOne(wrapper);
        CustomerGuestFamilyDetailDTO familyDetailDTO = new CustomerGuestFamilyDetailDTO();
        if (familyDetailDO != null)
            CopyNonNullBeanUtils.copyProperties(familyDetailDO, familyDetailDTO);
        return familyDetailDTO;
    }
}
